package com.chenss.dragonser.activity;

import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chenss.dragonser.R;
import com.chenss.dragonser.app.AppInfo;
import com.chenss.dragonser.app.Constant;
import com.chenss.dragonser.base.BaseActivity;
import com.chenss.dragonser.model.UserInfoModel;
import com.chenss.dragonser.network.NetworkRequest;
import com.chenss.dragonser.network.RequestCallbcak;
import com.chenss.dragonser.response.UserBeanResponse;
import com.chenss.dragonser.utils.GsonUtil;
import com.chenss.dragonser.utils.RegexValidateUtil;
import com.chenss.dragonser.utils.ToastUtil;

import org.json.JSONException;

import java.util.logging.Logger;

public class LoginActivity extends BaseActivity implements RequestCallbcak {
    Button btn_login;//登录
    EditText edt_phone;//电话
    EditText edt_pwd;//密码
    public NetworkRequest networkRequest;//网络请求
    public ArrayMap<String, String> parmMap;//请求参数封装

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView(View view) {
        edt_phone = findView(R.id.edt_phone);
        btn_login = findView(R.id.btn_login);
        edt_pwd = findView(R.id.edt_pwd);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void doBusiness(Context mContext) {
        networkRequest = new NetworkRequest(mContext, this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String phone = edt_phone.getText().toString();
                String pwd = edt_pwd.getText().toString();

                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showShort(this, "请输入帐号");
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showShort(this, "请输入密码");
                    return;
                }
                if (!RegexValidateUtil.checkCellphone(phone)) {
                    ToastUtil.showShort(this, "手机号码格式不正确");
                    return;
                }

                doLogin();
                break;
        }
    }

    /**
     * 登录
     */
    private void doLogin() {
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.Store, Constant.classes.StoreLogin);
        parmMap.put("phone", edt_phone.getText().toString());
        parmMap.put("password", edt_pwd.getText().toString());
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_1, true, Constant.classes.StoreLogin);
    }

    @Override
    public void onFaild(int orderCode, String result) {
        ToastUtil.showShort(this, result);
    }

    @Override
    public void onSucceed(int orderCode, String result) throws JSONException {
        Logger.getLogger(result);
        if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_1) {
            UserBeanResponse response = GsonUtil.GsonToBean(result, UserBeanResponse.class);
            if (null != response) {

                if (0 == response.getStatus()) {
                    //登录成功
                        AppInfo.setUser(this, response.getData());
                    ToastUtil.showShort(this, "登录成功");
                    startActivity(MainActivity.class);
                    this.finish();
//
//                    JPushInterface.setAliasAndTags(mContext, AppInfo.getUser(mContext).getStore_id(),null, new TagAliasCallback() {
//                        @Override
//                        public void gotResult(int i, String s, Set<String> set) {
//                            if (i == 0) {
//                                com.orhanobut.logger.Logger.d("别名设置成功");
//                            } else {
//                                com.orhanobut.logger.Logger.d("别名设置失败" + i);
//                            }
//                        }
//                    });
//                    EventBus.getDefault().post(new LoginEvent(1));
                }


            }


        }
    }
}
